// API - 访问用户控制

import type { Response } from '@/config/index'
import { http } from '@/utils/http'

export interface UserTokenResult extends Response {
  data: UserToken
}

export interface UserResult extends Response {
  data: UserInfo
}

/**
 * 用户登录（手机）
 *
 * @param data 请求参数
 * @returns A promise resolving to the user's token result.
 */
export function userPhoneLogin(data?: object) {
  return http.request<UserTokenResult>('post', '/access/login/phone', {
    data,
  })
}

/**
 * 用户登录（账密）
 *
 * @param data 请求参数
 * @returns A promise resolving to the user's information.
 */
export function userAccountLogin(data?: object) {
  return http.request<UserTokenResult>('post', '/access/login/account', {
    data,
  })
}

/**
 * 发送登录验证
 *
 * @param data 请求参数
 * @returns A promise resolving to the response of the login SMS request.
 */
export function sendUserLoginSms(data?: object) {
  return http.request<Response>('post', '/access/login/send-sms', {
    data,
  })
}

/**
 * 获取已登录用户信息
 *
 * @returns A promise resolving to the refreshed access token result.
 */
export function getUserInfo() {
  return http.request<UserResult>('get', '/access/info')
}

/**
 * 刷新Token
 *
 * @param data 请求参数
 * @returns A promise resolving to the refreshed access token result.
 */
export function refreshAccessToken(data?: object) {
  return http.request<UserTokenResult>(
    'post',
    '/access/refresh-token',
    {
      data,
    },
    { quite: true },
  )
}
